package com.atguigu.yygh.order.listener;


import com.atguigu.mq.consts.MqConst;
import com.atguigu.mq.service.RabbitService;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.mapper.OrderInfoMapper;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TaskListener {

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    RabbitService rabbitService;


    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(name = MqConst.QUEUE_TASK_8,durable = "true"),
                            exchange = @Exchange(name = MqConst.EXCHANGE_DIRECT_TASK,durable = "true"),
                            key = MqConst.ROUTING_TASK_8
                    )
            }
    )
    public void aa(String reserve_date){
        System.out.println("第三个队列中的消息：" + reserve_date);

        //根据日期(订单状态为已支付)，查询指定的订单列表
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reserve_date",reserve_date);
        queryWrapper.eq("order_status", OrderStatusEnum.PAID.getStatus());

        List<OrderInfo> list = orderInfoMapper.selectList(queryWrapper);

        for (OrderInfo orderInfo : list) {
//            System.out.println(orderInfo);

            MsmVo msmVo = new MsmVo();
            msmVo.setPhone(orderInfo.getPatientPhone());
            String message = "【尚医通】" + orderInfo.getPatientName() +"您好！" + "明天是就诊日，请及时就诊";
            msmVo.getParam().put("message",message);

            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);

        }

        //每个订单封装一个msmVo，发送到第二个队列中（短信服务监听第二个队列）

    }

}
